﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;
using ControlLogNamespace;

namespace BackstageMusiciansFriend
{
    public class ControlImagen
    {
        static string nCarpeta;
        
        private ControlImagen()
        {
        }

        private static int ParsearImagenes(string cadena, ref string[] sImgs)
        {
            int i = 0,
                j = 0,
                iCant = 0,
                aux = cadena.Length;

            while ((i < aux) && (iCant<3))
            {
                j = i;
                do
                {
                    i++;
                } while ((i < aux) && (cadena[i] != ','));

                sImgs[iCant] = cadena.Substring(j, i - j);

                i++;
                iCant++;
            }
            
            return iCant;
        }
        
        public static void DescargarImagenes(ProductML[] prod, int iCantProd, string carpeta, string usuario)
        {
            int i,
                j,
                iCantImg;
            
            string sProd,
                   sImg;
            
            Image img;
            HttpWebRequest requestForImage = null;
            WebResponse responseForImage = null;
            Stream streamForImage = null;

            string saveDir = @"Upload_Img_" + usuario + "\\";

            string[] sImgs = new string[3];

            nCarpeta = carpeta + saveDir;

            ControlImagen.CrearCarpetaTemp();

            for (i = 0; i < iCantProd; i++)
            {
                iCantImg = ParsearImagenes(prod[i].Images, ref sImgs);

                for (j = 0; j < iCantImg; j++)
                {
                    requestForImage = (HttpWebRequest)HttpWebRequest.Create(sImgs[j]);
                    requestForImage.AllowWriteStreamBuffering = true;

                    responseForImage = requestForImage.GetResponse();

                    streamForImage = responseForImage.GetResponseStream();

                    img = Image.FromStream(streamForImage);

                    if (i < 10)
                        sProd = "000" + i.ToString();
                    else
                        sProd = "00" + i.ToString();

                    sImg = "000" + j.ToString();

                    img.Save(nCarpeta + sProd + "-" + sImg + ".jpg");

                    img.Dispose();
                    responseForImage.Close();
                    streamForImage.Close();
                }
            }
            
        }

        public static bool ObtenerImagen(string sFile, ref string sPath)
        {
            string sDest = nCarpeta + "\\" + sFile + ".jpg";

            try
            {
                FileStream stream = new FileStream(sDest, FileMode.Open, FileAccess.Read);
                sPath = sDest;
                stream.Close();
                return true;
            }
            catch (Exception Ex)
            {
                return false;
            }
        }

        public static void BorrarCarpetaTemp()
        {
            try
            {
                ControlLog.writeLog("[BorrarCarpetaTemp] Borrando directorio = " + nCarpeta);
                Directory.Delete(nCarpeta, true);
            }
            catch (Exception Ex)
            {
                ControlLog.writeLog("[BorrarCarpetaTemp] Excepcion borrando directorio = " + nCarpeta + ":" + Ex.Message);
              
            }
        }

        public static void CrearCarpetaTemp()
        {
            try
            {
                if (Directory.Exists(nCarpeta) == false)
                {

                    ControlLog.writeLog("[CrearCarpetaTemp] Creando directorio = " + nCarpeta);
                    Directory.CreateDirectory(nCarpeta);
                }
                else
                {
                    ControlLog.writeLog("[CrearCarpetaTemp] Creando directorio = " + nCarpeta);
                }
            }
            catch (Exception Ex)
            {
                ControlLog.writeLog("[CreandoCarpetaTemp] Excepcion creando directorio = " + nCarpeta + ":"+Ex.Message);
            }
        }
    }
}